<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateFn">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item command="delete" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentApi } from '@/api/department'
export default {
  props: {
    treeNode: {
      type: Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 部门操作
    operateFn(command) {
      if (command === 'delete') {
        this.delDepts()
      } else if (command === 'add') {
        this.addDepts()
      } else {
        this.editDepts()
      }
    },
    // 编辑部门
    editDepts() {
      this.$emit('editDepts', this.treeNode)
    },
    // 新增部门
    addDepts() {
      this.$emit('addDepts', this.treeNode)
    },
    // 删除部门
    async delDepts() {
      // 删除前进行二次确认
      await this.$confirm('此操作将删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // 删除逻辑
      await delDepartmentApi(this.treeNode.id)
      this.$message.success('删除成功')
      this.$emit('success')
    }
  }
}
</script>

<style>
</style>